package com.htfyun.roobofactory.factorytest;

import com.htfyun.roobofactory.BaseActivity;
import com.htfyun.roobofactory.R;
import com.htfyun.roobofactory.ViewDialogFragment;
import com.htfyun.roobofactory.utils.PermissionManager;
import com.htfyun.roobofactory.view.CameraPreview;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import java.io.IOException;

/**
 * Created by sst on 2018/7/8.
 */

public class CameraTestActivity extends BaseActivity {

    private Camera mCamera;
    private Camera.Parameters parameters;

    private Context mContext;

    private Button btnPass;
    private Button btnFail;

    private FrameLayout previewContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;

        setContentView(R.layout.activity_factorytest_camera);

        previewContainer = (FrameLayout) findViewById(R.id.camera_preview);

        btnPass = (Button) findViewById(R.id.btnPass);
        btnFail = (Button) findViewById(R.id.btnFail);
        btnPass.setOnClickListener(onPassOrFailClickListener);
        btnFail.setOnClickListener(onPassOrFailClickListener);


    }

    @Override
    protected void onResume() {
        super.onResume();

        initPermission();

    }


    private void initPermission() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

            //同时申请多个权限
            PermissionManager.getInstance(getApplicationContext()).execute(this,
                    Manifest.permission.CAMERA
            );

        } else {

            initCameraPreview();

        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.getInstance(getApplicationContext()).
                onRequestPermissionsResult(requestCode, permissions, grantResults);

        Log.e(TAG, "mic ~~~~~~~~~~~~~~onRequestPermissionsResult");

        initCameraPreview();

    }


    @Override
    protected void onPause() {
        super.onPause();
        deinitCameraPreview();
    }

    private void initCameraPreview() {
        if (mCamera != null) {
            deinitCameraPreview();
        }

        mCamera = getCameraInstance();

        if (mCamera == null) {
            Toast.makeText(mContext, R.string.camera_not_found, Toast.LENGTH_LONG).show();
            setResult(RESULT_CANCELED);
            ActivityCompat.finishAfterTransition(getActivity());
            return;
        }

        parameters = mCamera.getParameters();

        parameters.setJpegQuality(100);
        parameters.setFlashMode("off");
        parameters.setPictureFormat(256);
        parameters.setPreviewFormat(17);
        parameters.setPictureSize(640, 480);
        parameters.setPreviewSize(1280, 720);
        parameters.setFlashMode("off");
        parameters.setRecordingHint(true);
        parameters.setWhiteBalance("auto");
        parameters.setSceneMode("auto");
        parameters.setPreviewFpsRange(0, 30);

        if (parameters.getSupportedFocusModes().contains("continuous-video")) {
            parameters.setFocusMode("continuous-video");
            // Create our Preview viewand set it as the content of our activity.
        }
        mCamera.setParameters(parameters);

        previewContainer.addView(new CameraPreview(this, mCamera));
    }

    private void deinitCameraPreview() {
        if (mCamera != null) {
            mCamera.release();

            previewContainer.removeAllViews();

            mCamera = null;
        }
    }

    public static Camera getCameraInstance() {
        Camera c = null;
        try {
            c = Camera.open(0); // attempt to get a Camera instance
        } catch (Exception e) {
            // Camera is notavailable (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }

    private View.OnClickListener onPassOrFailClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btnPass) {

                setResult(RESULT_OK);
                ActivityCompat.finishAfterTransition(getActivity());

            } else if (v == btnFail) {

                setResult(RESULT_CANCELED);
                ActivityCompat.finishAfterTransition(getActivity());

            }
        }
    };

}
